﻿using System;
using System.Data;
using System.Drawing;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using HiauPdm.Bll;
using HiauPdm.Web;

namespace HiauPdm.Web
{
    public partial class CpSjCk : PageBase
    {
        protected override bool IsAccess()
        {
            return (IsAccess("CpSjBj.aspx") | IsAccess("CpSjCx.aspx"));
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BtnExport.Visible = IsAccess("导出产品设计明细表");
                TextId.Value = Request.QueryString["CpSjId"];
                BindData();
            }
        }

        private void BindData()
        {
            if ((TextId.Value.Trim() != string.Empty) && BGlobal.IsNumeric(TextId.Value.Trim()))
            {
                DataSet ds = BCpSjCx.GetCpSjCxByCpSjId(Convert.ToInt32(TextId.Value.Trim()));
                if ((ds != null) && (ds.Tables[0].Rows.Count == 1))
                {
                    PanelEdit.Visible = true;
                    TextDh.Text = ds.Tables[0].Rows[0]["代号"].ToString();
                    TextLb.Text = ds.Tables[0].Rows[0]["类别"].ToString();
                    TextMc.Text = ds.Tables[0].Rows[0]["名称"].ToString();
                    TextGgxh.Text = ds.Tables[0].Rows[0]["规格型号"].ToString();
                    TextDw.Text = ds.Tables[0].Rows[0]["单位"].ToString();
                    TextBz.Text = ds.Tables[0].Rows[0]["备注"].ToString();
                    TextDjr.Text = ds.Tables[0].Rows[0]["登记人"].ToString();
                    TextBjr.Text = ds.Tables[0].Rows[0]["编辑人"].ToString();
                    TextBjsj.Text = ds.Tables[0].Rows[0]["编辑时间"].ToString();
                    TextDl.Value = ds.Tables[0].Rows[0]["大类"].ToString();
                    BindSjwdData(Convert.ToInt32(TextId.Value.Trim()));
                    BindCpSjLbjData(Convert.ToInt32(TextId.Value.Trim()));
                    BindCpSjWgjData(Convert.ToInt32(TextId.Value.Trim()));
                }
            }
        }

        private void BindSjwdData(int iCpSjId)
        {
            DataSet ds = BCpSjBj.GetCpSjWdBySjId(iCpSjId);
            if ((ds != null) && (ds.Tables[0].Rows.Count > 0))
            {
                PanelEditWd.Visible = true;
            }
            GridCpSjWd.DataSource = ds;
            GridCpSjWd.DataBind();
            if (((TextDl.Value == "1" || TextDl.Value == "2") && !IsAccess("整件及部件文档下载")) || (TextDl.Value == "0" && !IsAccess("零件及购件文档下载")))
            {
                for (int i = 0; i < GridCpSjWd.Rows.Count; i++)
                {
                    if (GridCpSjWd.Rows[i].Cells[3].Text.Trim() != (string)Session["RealName"])
                    {
                        GridCpSjWd.Rows[i].Cells[5].Controls[1].Visible = false;
                    }
                }
            }
        }

        private void BindCpSjLbjData(int iCpSjId)
        {
            DataSet ds = BCpSjBj.GetCpSjLbjBySjId(iCpSjId);
            if ((ds != null) && (ds.Tables[0].Rows.Count > 0))
            {
                PanelEditLbj.Visible = true;
            }
            GridCpSjLbj.DataSource = ds;
            GridCpSjLbj.DataBind();
        }

        private void BindCpSjWgjData(int iCpSjId)
        {
            DataSet ds = BCpSjBj.GetCpSjWgjBySjId(iCpSjId);
            if ((ds != null) && (ds.Tables[0].Rows.Count > 0))
            {
                PanelEditWgj.Visible = true;
            }
            GridCpSjWgj.DataSource = ds;
            GridCpSjWgj.DataBind();
        }

        protected void GridCpSjWd_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DownloadDoc")
            {
                Response.Redirect("DownloadFile.aspx?id=" + BGlobal.Encrypt(string.Format("{0}|{1}_{2}.{3}", GridCpSjWd.DataKeys[Convert.ToInt32(e.CommandArgument)].Values[0], TextDh.Text.Trim(), GridCpSjWd.Rows[Convert.ToInt32(e.CommandArgument)].Cells[1].Text.Trim(), BGlobal.HexToString(GridCpSjWd.DataKeys[Convert.ToInt32(e.CommandArgument)].Values[0].ToString().Split('_')[1])), "Download"));
            }
        }

        protected void BtnExport_Click(object sender, EventArgs e)
        {
            DataSet dsLbj = BCpSjBj.GetCpSjLbjBySjId(Convert.ToInt32(TextId.Value.Trim()));
            DataSet dsWgj = BCpSjBj.GetCpSjWgjBySjId(Convert.ToInt32(TextId.Value.Trim()));
            BCpSjCx.ExportCpSjMxb(this.Page, TextDh.Text, TextMc.Text, TextGgxh.Text, dsLbj, dsWgj);
        }
    }
}